<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:pho="http://www.hitachivantara.com">
<head>
<title>Test Harness</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<style  TYPE="text/css">
<!--

.document {
    font-family: Tahoma,Ariel;
    background-color:white;
}

.table {
    border: 1px solid #808080;
}

-->
</style>

</head>

<body onload="loadPage()" class="document">


<h3>Tests</h3>

<button onclick="testAll()">Test All</button>

<table id="testtable" border="0">
    <tbody>
    </tbody>
</table>

<script language="javascript" src="webcontext.js" type="text/javascript"></script>
<script type="text/javascript">

  $.extend(djConfig, { modulePaths: {
                          dataapi: "../../dataapi"
                      },
                      baseUrl: '../dojo/dojo/'
                  });

</script>

<script src="../dojo/dojo/dojo-all.js" type="text/javascript"></script>
<script src="../util/pentaho-csrf.js" type="text/javascript"></script>
<script src="../repo/pentaho-ajax.js" type="text/javascript"></script>
<script src="../repo/pentaho-thin-app.js" type="text/javascript"></script>
<script src="../dataapi/oop.js" type="text/javascript"></script>
<script src="../dataapi/app.js" type="text/javascript"></script>
<script src="../dataapi/controller.js" type="text/javascript"></script>
<script src="../dataapi/xhr.js" type="text/javascript"></script>
<script src="../dataapi/cda.js" type="text/javascript"></script>
<script src="../dataapi/models-mql.js" type="text/javascript"></script>
<script src="../dataapi/Xmla.js" type="text/javascript"></script>
<script src="../dataapi/models-olap.js" type="text/javascript"></script>
<script src="../dataapi/models-cda.js" type="text/javascript"></script>

<script type="text/javascript">

var models = new Array();
var model = null;

function loadPage() {

	var da_mql = {
		name: 'da-mql',
		objectClass: pentaho.pda.MqlHandler
	};
	pentaho.myapp = {
		app: new pentaho.pda.app(),
		prior_idx: -1,
		source: {}
	};
	var moduleArray = [da_mql];
	pentaho.myapp.app.init(moduleArray);
    pentaho.myapp.app.moduleData['da-mql'].instance.METADATA_SERVICE_URL = CONTEXT_PATH + 'content/ws-run/metadataService';

    // create the tests table
    var table = document.getElementById('testtable');

    var idx=1;
    var running = true;
    while( table.rows.length > 1 ) {
        table.deleteRow(1);
    }
    while(running) {
        try {
            eval('test'+idx);
            row = table.insertRow(-1);
            var html = '<td style="height:25px"><button onclick="test('+idx+')">Test '+idx+'</button></td>\n';
            html += '<td style="height:25px;background-color:#ffffff" id="teststatus'+idx+'">---</td>\n';
            html += '<td style="height:25px" id="testdesc'+idx+'">---</td>\n';
            addTableCells( row, html );
        } catch(e) {
            running = false;
        }
        idx++;
    }
//    test18();
}

function addTableCells( rowObj, html ) {
    var tmp = document.createElement("div");
    tmp.innerHTML = '<table><tr>'+html+'</tr></table>';
    tmp = tmp.childNodes[0].childNodes[0].childNodes[0].childNodes;
    while(tmp.length > 0 ) {
        rowObj.appendChild(tmp[0]);
    }
}


function reset(idx) {

    document.getElementById('teststatus'+idx).style.backgroundColor="#ffffff";
    document.getElementById('teststatus'+idx).innerHTML="---";
    document.getElementById('testdesc'+idx).innerHTML = "---";

}

function testAll() {

    var idx = 1;
    while(document.getElementById('teststatus'+idx) != null) {
        reset(idx);
        idx++;
    }
    idx = 1;
    var running = true;
    while(running) {
        try {
            eval('test('+idx+')');
            idx++;
        } catch (e) {
            running = false;
        }
    }
}

function test(idx) {
    try {
        document.getElementById('teststatus'+idx).style.backgroundColor="#ffffff";
        document.getElementById('teststatus'+idx).innerHTML="testing";
        document.getElementById('testdesc'+idx).innerHTML = "";
        eval('test'+idx+'()');
    } catch (e) {
        setResult(idx,'fail',e.message);
    }
}

function setResult( idx, status, message ) {
    document.getElementById('teststatus'+idx).innerHTML = status;
    document.getElementById('testdesc'+idx).innerHTML = message;
    document.getElementById('teststatus'+idx).style.backgroundColor=(status=='success') ? "#80ff80" : "#ff8080";
}

// test the 'get sources' call, make sure the Orders model exists
function test1() {

    if( getOrdersModel() == null) {
        setResult(1,'fail','Orders model not found');
        return;
    }
    setResult(1,'success','');

}

function getOrdersModel() {
    if(model != null) {
        return model;
    }
    // find the orders model
	pentaho.myapp.app.getSources(
		//function to be called for each source as it is added
		function(source) {
            models.push(source);
            if(source.id == 'steel-wheels:BV_ORDERS') {
                model = source;
            }
		}
	);
    return model;
}

function assertEquals(shouldBe, is, idx, comment) {
    if( shouldBe != is ) {
        setResult(idx,'fail',comment +" - Expected '"+shouldBe+"' but got '"+is+"'");
        return false;
    }
    return true;
}

// load the Orders model, test that expected columns exist
function test2() {

    pentaho.myapp.source = pentaho.myapp.app.getSources(null, {filter: {property:'id',
    value:'steel-wheels:BV_ORDERS'}});
    pentaho.myapp.source.discoverModelDetail();
    var elements = pentaho.myapp.source.getAllElements();

    if(!assertEquals("BC_CUSTOMER_W_TER_", elements[0].id, 2, 'Wrong id element for 1')) {
        return false;
    }
    if(!assertEquals("CATEGORY", elements[0].elementType, 2, 'Wrong element type for element 1')) {
        return false;
    }
    setResult(2,'success','');

}

// test a query with a condition 'country = Australia'
function test3() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.EQUAL,["Australia"], pentaho.pda.Column.OPERATOR_TYPES.AND);

    var obj = model.submitQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia']],3);
}

// test a query with a condition 'country < B' in ascending order
function test4() {query

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.LESS_THAN,["B"], pentaho.pda.Column.OPERATOR_TYPES.AND);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Austria']],4);
}

// test a query with a condition 'country < B' in descending order
function test5() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.LESS_THAN,["B"], pentaho.pda.Column.OPERATOR_TYPES.AND);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.DESCENDING );

    var obj = model.submitQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Austria'],['Australia']],5);
}

// test a query with a condition 'country = Australia or Belgium' in ascending order
function test6() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.EQUAL,["Australia"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.EQUAL,["Belgium"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Belgium']],6);
}

// test a query with a condition 'country <= Belgium' in ascending order
function test7() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.LESS_THAN_OR_EQUAL,["Belgium"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Austria'],['Belgium']],7);
}

// test a query with a condition 'country > Switzerland' in ascending order
function test8() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.MORE_THAN,["Switzerland"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['UK'],['USA']],8);
}

// test a query with a condition 'country >= Switzerland' in ascending order
function test9() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.MORE_THAN_OR_EQUAL,["Switzerland"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Switzerland'],['UK'],['USA']],9);
}

// test a query with a condition 'country contains land' in ascending order
function test10() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.CONTAINS,["land"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Finland'],['Ireland'],['Netherlands'],['New Zealand'],['Poland'],['Switzerland']],10);
}

// test a query with a condition 'country does not contain "a"' in ascending order
function test11() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.NOT_CONTAINS,["a"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Belgium'],['Hong Kong'],['Philippines'],['Sweden'],['UK'],['USA']],11);
}

// test a query with a condition 'country beigns with "A"' in ascending order
function test12() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.BEGINSWITH,["A"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Austria']],12);
}

// test a query with a condition 'country ends with "ia"' in ascending order
function test13() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.ENDSWITH,["ia"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Austria'],['Russia']],13);
}

// test a query with a condition 'country is null' in ascending order
function test14() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.IS_NULL,[""], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[],14);
}

// test a query with a condition 'country is not null' in ascending order
function test15() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.NOT_NULL,[""], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Austria'],['Belgium'],['Canada'],['Denmark'],['Finland'],
    ['France'],['Germany'],['Hong Kong'],['Ireland']
    ,['Israel'],['Italy'],['Japan'],['Netherlands'],['New Zealand'],['Norway'],['Philippines'],['Poland'],['Portugal'],['Russia']
    ,['Singapore'],['South Africa'],['Spain'],['Sweden'],['Switzerland'],['UK'],['USA']],15);
}

// test a query with a parameter
function test16() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.LESS_THAN,["BC_CUSTOMER_W_TER_COUNTRY"], pentaho.pda.Column.OPERATOR_TYPES.AND, true);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );
    query.addParameterById('BC_CUSTOMER_W_TER_COUNTRY','BC_CUSTOMER_W_TER_COUNTRY',["Belgium"]);

    var obj = model.submitQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Austria']],16);
}

// test a query with a row limit
function test17() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitQuery(query, 4);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Austria'],['Belgium'],['Canada']],17);
}

// test a query with a condition 'country = Australia'
function test18() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.EQUAL,["Australia"], pentaho.pda.Column.OPERATOR_TYPES.AND);

    var obj = model.submitXmlQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia']],18);
}

// test a query with a condition 'country < B' in ascending order
function test19() {query

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.LESS_THAN,["B"], pentaho.pda.Column.OPERATOR_TYPES.AND);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitXmlQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Austria']],19);
}

// test a query with a condition 'country < B' in descending order
function test20() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.LESS_THAN,["B"], pentaho.pda.Column.OPERATOR_TYPES.AND);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.DESCENDING );

    var obj = model.submitXmlQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Austria'],['Australia']],20);
}

// test a query with a condition 'country = Australia or Belgium' in ascending order
function test21() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.EQUAL,["Australia"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.EQUAL,["Belgium"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitXmlQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Belgium']],21);
}

// test a query with a condition 'country <= Belgium' in ascending order
function test22() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.LESS_THAN_OR_EQUAL,["Belgium"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitXmlQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Austria'],['Belgium']],22);
}

// test a query with a condition 'country > Switzerland' in ascending order
function test23() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.MORE_THAN,["Switzerland"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitXmlQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['UK'],['USA']],23);
}

// test a query with a condition 'country >= Switzerland' in ascending order
function test24() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.MORE_THAN_OR_EQUAL,["Switzerland"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitXmlQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Switzerland'],['UK'],['USA']],24);
}

// test a query with a condition 'country contains land' in ascending order
function test25() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.CONTAINS,["land"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitXmlQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Finland'],['Ireland'],['Netherlands'],['New Zealand'],['Poland'],['Switzerland']],25);
}

// test a query with a condition 'country does not contain "a"' in ascending order
function test26() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.NOT_CONTAINS,["a"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitXmlQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Belgium'],['Hong Kong'],['Philippines'],['Sweden'],['UK'],['USA']],26);
}

// test a query with a condition 'country beigns with "A"' in ascending order
function test27() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.BEGINSWITH,["A"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitXmlQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Austria']],27);
}

// test a query with a condition 'country ends with "ia"' in ascending order
function test28() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.ENDSWITH,["ia"], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitXmlQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Austria'],['Russia']],28);
}

// test a query with a condition 'country is null' in ascending order
function test29() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.IS_NULL,[""], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitXmlQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[],29);
}

// test a query with a condition 'country is not null' in ascending order
function test30() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.NOT_NULL,[""], pentaho.pda.Column.OPERATOR_TYPES.OR);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitXmlQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Austria'],['Belgium'],['Canada'],['Denmark'],['Finland'],
    ['France'],['Germany'],['Hong Kong'],['Ireland']
    ,['Israel'],['Italy'],['Japan'],['Netherlands'],['New Zealand'],['Norway'],['Philippines'],['Poland'],['Portugal'],['Russia']
    ,['Singapore'],['South Africa'],['Spain'],['Sweden'],['Switzerland'],['UK'],['USA']],30);
}

// test a query with a parameter
function test31() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addConditionById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.CONDITION_TYPES.LESS_THAN,["BC_CUSTOMER_W_TER_COUNTRY"], pentaho.pda.Column.OPERATOR_TYPES.AND, true);
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );
    query.addParameterById('BC_CUSTOMER_W_TER_COUNTRY','BC_CUSTOMER_W_TER_COUNTRY',["Belgium"]);

    var obj = model.submitXmlQuery(query);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Austria']],31);
}

// test a query with a row limit
function test32() {

    initModel();
    var query = model.createQuery();
    query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
    query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

    var obj = model.submitQuery(query, 4);
    testResults(obj,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia'],['Austria'],['Belgium'],['Canada']],32);
}

function test33() {
  initModel();
  var query = model.createQuery();
  query.addSelectionById('BC_CUSTOMER_W_TER_COUNTRY');
  query.addSortById('BC_CUSTOMER_W_TER_COUNTRY', pentaho.pda.Column.SORT_TYPES.ASCENDING );

  var callback = function(data) {
    testResults(data,['BC_CUSTOMER_W_TER_COUNTRY'],[['Australia']],33);
  }

  // Set the status to failed in cause this doesn't return
  setResult(33, 'fail', 'Callback not fired');
  var obj = model.submitQuery(query, 1, callback);
}

function test34() {
  // Validate we can filter by the aggregate of a field
  initModel();
  var query = model.createQuery();
  query.addSelectionById('BC_CUSTOMER_W_TER_TERRITORY');
  query.addSelectionById('BC_PRODUCTS_BUYPRICE');
  query.addSortById('BC_PRODUCTS_BUYPRICE', pentaho.pda.Column.SORT_TYPES.ASCENDING );
  query.addConditionById('BC_PRODUCTS_BUYPRICE', pentaho.pda.Column.CONDITION_TYPES.EQUAL, ['54.86'], pentaho.pda.Column.OPERATOR_TYPES.OR, undefined, pentaho.pda.Column.AGG_TYPES.AVERAGE);

  var obj = model.submitXmlQuery(query);
  testResults(obj,['BC_CUSTOMER_W_TER_TERRITORY', 'BC_PRODUCTS_BUYPRICE'],[['NA', '58916.55']],34);
}

function initModel() {
    if(model == null || model.getAllColumns().length == 0 ) {
        pentaho.myapp.source = pentaho.myapp.app.getSources(null, {filter: {property:'id',
        value:'steel-wheels/metadata.xmi:BV_ORDERS'}});
        pentaho.myapp.source.discoverModelDetail();
        model = pentaho.myapp.source;
    }
}

function testResults( obj, columnNames, cells, idx ) {
    try {
        if(obj == null) {
            setResult(idx,'fail','JSON results are null');
            return false;
        }
        if(!assertEquals(columnNames.length, obj.metadata.length, idx, 'Wrong number of columns')) {
            return false;
        }
        if(!assertEquals(cells.length, obj.resultset.length, idx, 'Wrong number of rows')) {
            return false;
        }
        for( var i=0; i<columnNames.length; i++ ) {
            if(!assertEquals(columnNames[i], obj.metadata[i].colName, idx, 'Column name is wrong')) {
                return false;
            }
        }
        for( var i=0; i<cells.length; i++ ) {
            for(var j=0; j<cells[i].length; j++ ) {
                if( !assertEquals(cells[i][j], obj.resultset[i][j], idx, 'Cell value is wrong')) {
                    return false;
                }
            }
        }

    } catch (e) {
            setResult(idx,'fail','Exception: '+e.message);
            return false;
    }
    setResult(idx,'success','');
    return true;
}

</script>

</body>

</html>
